package com.example.ordersystem;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.ordersystem.pojo.User;
import com.example.ordersystem.utils.ListDataSave;

import java.util.List;

/*
 * 用户信息页面
 */
public class UserFragment extends Fragment {
    boolean isLogin = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.userpage, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final Button user_btn_login = getActivity().findViewById(R.id.user_btn_login);
        final TextView user_textView_statement = getActivity().findViewById(R.id.user_textView_statement);
        Button user_loginOut = getActivity().findViewById(R.id.user_loginOut);
        Button user_old_order = getActivity().findViewById(R.id.user_old_order);
        Button user_info = getActivity().findViewById(R.id.user_info);
        TextView textView_phone = getActivity().findViewById(R.id.textView11_user_phone);
        TextView textView_address = getActivity().findViewById(R.id.textView13_user_address);
        TextView textView_username = getActivity().findViewById(R.id.textView9_user_username);

        user_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                startActivity(intent);
            }
        });


        //用户界面的登录按钮
        user_btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });

        final LinearLayout user_message = getActivity().findViewById(R.id.user_message);
        //用户界面的退出登录按钮
        user_loginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListDataSave.putUserLogin(getContext(), "LoginOut");
                isLogin = false;
                user_textView_statement.setText("    用户未登录");
                user_btn_login.setVisibility(View.VISIBLE);
                user_message.setVisibility(View.GONE);
                Intent intent = new Intent(getContext(), MainActivity.class);
                getContext().startActivity(intent);

            }
        });


        user_old_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),"进入用户的历史订单",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), OldOrderActivity.class);
                getContext().startActivity(intent);
            }
        });

        String username = null;
        String phone = null;
        String address = null;

        String userLogin = ListDataSave.getUserLogin(getContext());
        if (!userLogin.equals("LoginOut")) {
            isLogin = true;
            List<User> userList = ListDataSave.getUserList(getContext(), userLogin);
            try {
                for (User user : userList) {
                    if (userLogin.equals(user.getName())) {
                        phone = user.getPhone();
                        address = user.getAddress();
                        username = user.getName();
                    }
                }

                if (isLogin) {
                    user_textView_statement.setText("     用户在线");
                    user_btn_login.setVisibility(View.GONE);
                    textView_username.setText(" "+username);
                    textView_phone.setText(phone);
                    textView_address.setText(address);
                    user_message.setVisibility(View.VISIBLE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
